package com.asc.service.user.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.asc.common.entity.TUUser;
import com.asc.service.user.entity.UserEXT;
import com.asc.service.user.service.UserService;
import com.asc.utils.HTMLUtil;
import com.asc.utils.ObjectUtil;
import com.asc.utils.Page;
import com.asc.utils.ResultWrap;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;

	//登录
	@RequestMapping("/login")
	public void test(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> requestMap = HTMLUtil.getPageParams(request);
		ResultWrap resultWrap = HTMLUtil.getDefaultWrap();
		if(ObjectUtil.isNotBlank(requestMap) && ObjectUtil.isNotBlank(requestMap.get("account")) && ObjectUtil.isNotBlank(requestMap.get("password"))) {
			UserEXT user = userService.login(requestMap);
			if(ObjectUtil.isNotBlank(user)) {
				resultWrap.setData(user);
			}else {
				resultWrap.setFlag(0);
				resultWrap.setMessage("用户名或密码错误");
			}
		}else {
			resultWrap.setParamIsnotEmpty("用户名或密码不能为空");
		}
		HTMLUtil.sendWrap(response, resultWrap, request);
	}
	
	//查询用户
	@RequestMapping("/queryUser")
	public void queryUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> requestMap = HTMLUtil.getPageParams(request);
		ResultWrap resultWrap = HTMLUtil.getDefaultWrap();
		List<UserEXT> users = userService.queryUser(requestMap);
		resultWrap.setData(users);
		int resultCNT = userService.queryUserCount(requestMap);
		resultWrap.setiTotalDisplayRecords(resultCNT);
		resultWrap.setiTotalRecords(resultCNT);
		HTMLUtil.sendWrap(response, resultWrap, request);
	}
	
	//删除用户,更改delstate状态
	@RequestMapping("/deleteUser")
	public void deleteUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> requestMap = HTMLUtil.getPageParams(request);
		ResultWrap resultWrap = HTMLUtil.getDefaultWrap();
		requestMap.put("delstate", 0);
		int result = userService.updateUser(requestMap);
		if(result >= 1) {
			resultWrap.setDeleteSuccess();
		}else {
			resultWrap.setDeleteFailure();
		}
		HTMLUtil.sendWrap(response, resultWrap, request);
	}
	
}
